package lolxxs.chenyanting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lolxxs.chenyanting.model.Log;
import lolxxs.chenyanting.mapper.LogMapper;
import lolxxs.chenyanting.service.ILogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lolxxs.chenyanting.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2021-12-30
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public int add(Log log) {
//        Date date = new Date();
//        log.setLogId(idWorker.nextId());
//        log.setCreateTime(date);
//        log.setUpdateTime(date);
//        log.setState(1);
        return 0;
//        return logMapper.insert(log);
    }

    @Override
    public int update(Log log) {
        return 0;
    }

    @Override
    public List<Log> query(Log log) {
        List<Log> logs = logMapper.selectList(queryWrapper(log));
        if (logs.size() > 0) {
            return logs;
        }
        return null;
    }

    @Override
    public int delete(Log log) {
        return 0;
    }


    private QueryWrapper<Log> queryWrapper(Log log) {
        QueryWrapper<Log> wrapper=new QueryWrapper<>();
        if (log.getLogId() != null) {
            wrapper.eq("logId", log.getLogId());
        }
        if (log.getUserId() != null) {
            wrapper.eq("userId", log.getUserId());
        }
        if (log.getUrl() != null) {
            wrapper.like("url", log.getUrl());
        }
        if (log.getUserName() != null) {
            wrapper.eq("userName", log.getUserName());
        }
        if (log.getState() != null) {
            wrapper.eq("state", log.getState());
        }
        if (log.getCreateTime() != null) {
            // 大于左边时间
            wrapper.ge("createTime", log.getCreateTime());
        }
        if (log.getUpdateTime() != null) {
            // 小于右边时间
            wrapper.le("createTime", log.getUpdateTime());
        }
        if (log.getIp() != null) {
            wrapper.like("ip", log.getIp());
        }
        return wrapper;
    }
}
